{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6bf3da27-ee52-4e85-8a00-85a35113670b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "🦜 : db file dump to \u001b[94m/home/me/repo/msv2/front2/14/.tmp/db.json\u001b[0m\n",
      "🦜 : db: \u001b[94m{\n",
      "    \"f0.m\": {\n",
      "        \"8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4\": [\n",
      "            \"/home/me/repo/msv2/front2/14/.tmp/f0-a.m\"\n",
      "        ],\n",
      "        \"e9058ab198f6908f702111b0c0fb5b36f99d00554521886c40e2891b349dc7a1\": [\n",
      "            \"/home/me/repo/msv2/front2/14/.tmp/f0-b.m\",\n",
      "            \"/home/me/repo/msv2/front2/14/.tmp/f0-c.m\"\n",
      "        ]\n",
      "    },\n",
      "    \"f1.m\": {\n",
      "        \"e9058ab198f6908f702111b0c0fb5b36f99d00554521886c40e2891b349dc7a1\": [\n",
      "            \"/home/me/repo/msv2/front2/14/.tmp/f1-a.m\",\n",
      "            \"/home/me/repo/msv2/front2/14/.tmp/f1-b.m\"\n",
      "        ],\n",
      "        \"8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4\": [\n",
      "            \"/home/me/repo/msv2/front2/14/.tmp/f1-c.m\"\n",
      "        ]\n",
      "    },\n",
      "    \"f2.m\": {\n",
      "        \"e9058ab198f6908f702111b0c0fb5b36f99d00554521886c40e2891b349dc7a1\": [\n",
      "            \"/home/me/repo/msv2/front2/14/.tmp/f2-a.m\"\n",
      "        ],\n",
      "        \"8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4\": [\n",
      "            \"/home/me/repo/msv2/front2/14/.tmp/f2-b.m\",\n",
      "            \"/home/me/repo/msv2/front2/14/.tmp/f2-c.m\"\n",
      "        ]\n",
      "    }\n",
      "}\u001b[0m\n",
      "🦜 : mocked c api doc file dump to \u001b[94m/home/me/repo/msv2/front2/14/.tmp/mocked_c_api_doc.json\u001b[0m\n",
      "🦜 : mocked matlab api doc file dump to \u001b[94m/home/me/repo/msv2/front2/14/.tmp/mocked_matlab_api_doc.json\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "185"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "from hashlib import sha256\n",
    "from collections import defaultdict\n",
    "from random import choice\n",
    "\n",
    "class S:\n",
    "    BLUE = '\\033[94m'\n",
    "    CYAN = '\\033[96m'\n",
    "    GREEN = '\\033[92m'\n",
    "    NOR = '\\033[0m'\n",
    "\n",
    "    def print(s, *args):\n",
    "        print(s.format(S.BLUE, S.NOR, *args))\n",
    "\n",
    "def f(s: str, p = Path.cwd()):                  # simulate three files\n",
    "    ps = [p / f'{s}-a.m', p / f'{s}-b.m', p / f'{s}-c.m']\n",
    "    ps += ([p.with_suffix('.c') for p in ps] + [p.with_suffix('.h') for  p in ps])\n",
    "    w = ['hi', 'yo']\n",
    "    # 1. simulate the source file\n",
    "    # ps[0].write_text('hi')\n",
    "    # ps[1].write_text('yo')\n",
    "    # ps[2].write_text('hi')\n",
    "    for p in ps:\n",
    "        p.write_text(choice(w))\n",
    "\n",
    "    # 2. get the hashes\n",
    "    h = lambda x: sha256(x.read_text().encode()).hexdigest()\n",
    "    hs = [h(p) for p in ps]\n",
    "\n",
    "    o = defaultdict(list)\n",
    "    for h, p in zip(hs, ps):\n",
    "        o[h].append(p.as_posix()) if p.suffix == '.m' else None\n",
    "\n",
    "    return f'{s}.m', o\n",
    "\n",
    "tmp = Path.cwd() / '.tmp'\n",
    "tmp.mkdir() if not tmp.exists() else None\n",
    "o = {}\n",
    "# 01. make dir\n",
    "for i in range(3):\n",
    "    s, r = f(f'f{i}', tmp)\n",
    "    o[s] = r\n",
    "\f",
    "\n",
    "# 02. store json\n",
    "import json\n",
    "p0 = tmp / Path('db.json')\n",
    "p0.write_text(json.dumps(o, indent=4))\n",
    "S.print('🦜 : db file dump to {0}{2}{1}', p0)\n",
    "\n",
    "# 03. show the json\n",
    "S.print('🦜 : db: {0}{2}{1}', p0.read_text())\n",
    "\n",
    "# --------------------------------------------------\n",
    "# <2025-05-29 Thu> Make more mocked files\n",
    "# 4.1.\n",
    "d = {f'mocked_f{i}c' : f'<obj for mocked f{i}c>' for i in range(4)}\n",
    "p0 = tmp / Path('mocked_c_api_doc.json')\n",
    "p0.write_text(json.dumps(d, indent=4))\n",
    "S.print('🦜 : mocked c api doc file dump to {0}{2}{1}', p0)\n",
    "\n",
    "d = {f'mocked_f{i}m' : f'<obj for mocked f{i}m>' for i in range(4)}\n",
    "p0 = tmp / Path('mocked_matlab_api_doc.json')\n",
    "p0.write_text(json.dumps(d, indent=4))\n",
    "S.print('🦜 : mocked matlab api doc file dump to {0}{2}{1}', p0)\n",
    "\n",
    "\n",
    "# the mocked template\n",
    "s = '''\n",
    "matlab name:\n",
    "{m_name}\n",
    "\n",
    "matlab code:\n",
    "{m_code}\n",
    "\n",
    "header name:\n",
    "{h_name}\n",
    "\n",
    "header code:\n",
    "{h_code}\n",
    "\n",
    "matlab doc:\n",
    "{m_doc}\n",
    "\n",
    "c doc:\n",
    "{c_doc}\n",
    "\n",
    "rendered_headers:\n",
    "{rendered_headers}\n",
    "\n",
    "c name:\n",
    "{c_name}\n",
    "'''\n",
    "p0 = tmp / Path('mocked-template.txt')\n",
    "p0.write_text(s)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
